package com.example.lambda.test;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class lambda {


    @Test
    public void test(){
        Comparator<Integer> com = (x, y)->Integer.compare(x,y);
    }


    List<Employer>  lists = Arrays.asList(
            new Employer("张三",18,99999),
            new Employer("李四",20,44444),
            new Employer("王五",50,55555),
            new Employer("赵六",38,66666),
            new Employer("田七",28,77777)
    );


    public List<Employer> filter(List<Employer> list){
        List<Employer> lists = new ArrayList<>();
        for (Employer employer:
             list) {
            if(employer.getAge()>35){
                lists.add(employer);
            }
        }
        return lists;
    }

    @Test
    public void test2(){
        List<Employer> employers= filter(lists);
        for (Employer e:
             employers) {
            System.out.println(e);
        }
    }

    public List<Employer> filterByAge(List<Employer>list,MyPredicate<Employer> myPredicate){
        List<Employer> list1 = new ArrayList<>();
        for (Employer e:
             list) {
            if(myPredicate.test(e)){
                list1.add(e);
            }
        }
        return list1;
    }


    @Test
    public void test3(){
        List<Employer> list2= filterByAge(lists,new FilterByAge());
        for (Employer employer:
             list2) {
            System.out.println(employer);
        }
    }

    @Test
    public void test4(){
        List<Employer> list2= filterByAge(lists, new MyPredicate<Employer>() {
            @Override
            public boolean test(Employer employer) {
                return employer.getAge()>35;
            }
        });
        for (Employer e:list2){
            System.out.println(e);
        }
    }

    @Test
    public void test5(){
        List<Employer> list2 = filterByAge(lists,x->x.getAge()>35);
        for (Employer e : list2) System.out.println(e);
    }

    @Test
    public void test6(){
        lists.stream().filter(x->x.getAge()>35).forEach(System.out::println);
        lists.stream().map(Employer::getName).forEach(System.out::println);
    }
}
